<?php

namespace app\meizhuangapi\controller\user;

use app\meizhuangapi\controller\Controller;

use app\meizhuangapi\model\Order as OrderModel;
use app\meizhuangapi\model\Setting as SettingModel;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\enum\order\PayType as PayTypeEnum;
use app\common\service\qrcode\Extract as ExtractQRcode;
use think\Db;
use app\meizhuangapi\model\wxapp\Jssdk as jssdkModel;

use app\api\controller\user\Order as OrdersModel;
/**
 * 用户订单管理
 * Class Order
 * @package app\api\controller\user
 */
class Order extends OrdersModel
{
    /**
     * 我的订单列表
     * @param $dataType
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $dataType=input('post.dataType');
        $goods_name=input('post.goods_name');
        $wxapp_id=input('post.wxapp_id');
        $model = new OrderModel;
        $list = $model->getList($this->user['user_id'], $dataType,$goods_name);
        $storeInfo=Db::name('store_shop')->where(['wxapp_id'=>$wxapp_id])->find();
        //获取头像，资质图片
        $tx=Db::name('upload_file')->where(['file_id'=>$storeInfo['logo_image_id']])->find();
        //$zz=Db::name('upload_file')->where(['file_id'=>$storeInfo['image_id_yyzz']])->find();
        $storeInfo['headImageUrl']=$tx['file_url'].'/'.$tx['file_name'];
        return $this->renderSuccess(compact('list','storeInfo'));
    }

    /**
     * 订单详情信息
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail(){
        // 订单详情
        $wxapp_id=input('post.wxapp_id');
        $order_id=input('post.order_id');
        $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
        // 该订单是否允许申请售后
        $model['isAllowRefund'] = $model->isAllowRefund();
        //获取店铺头像，名称
        $storeInfo=Db::name('store_shop')->where(['wxapp_id'=>$wxapp_id])->find();
        //获取头像，资质图片
        $tx=Db::name('upload_file')->where(['file_id'=>$storeInfo['logo_image_id']])->find();
        //$zz=Db::name('upload_file')->where(['file_id'=>$storeInfo['image_id_yyzz']])->find();
        $storeInfo['headImageUrl']=$tx['file_url'].'/'.$tx['file_name'];
        $model['storeInfo']=$storeInfo;
        return $this->renderSuccess([
            'order' => $model,  // 订单详情
            'setting' => [
                // 积分名称
                'points_name' => SettingModel::getPointsName(),
            ],
        ]);
    }

    /**
     * 获取物流信息
     * @param $order_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function express(){
        $order_id=input('post.order_id');
        // 订单信息
        $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
        if (!$order['express_no']) {
            return $this->renderError('没有物流信息');
        }
        // 获取物流信息
        /* @var \app\store\model\Express $model */
        $model = $order['express'];
        $express = $model->dynamic($model['express_name'], $model['express_code'], $order['express_no']);
        if ($express === false) {
            return $this->renderError($model->getError());
        }
        return $this->renderSuccess(compact('express'));
    }

    /**
     * 取消订单
     */
    public function cancel(){
        $order_id=input('post.order_id');
        $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
        if ($model->cancel($this->user)) {
            return $this->renderSuccess([],'订单取消成功');
        }
        return $this->renderError([],'订单取消失败');
    }

    /**
     * 确认收货
     */
    public function receipt(){
        $order_id=input('post.order_id');
        $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
        if ($model->receipt()) {
            return $this->renderSuccess('收货成功');
        }
        return $this->renderError($model->getError());
    }

    /**
     * 获取订单核销二维码
     */
    public function extractQrcode(){
        $order_id=input('post.order_id');
        //订单详情
        $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
        //判断是否为待核销订单
        /*if (!$order->checkExtractOrder($order)) {
            return $this->renderError($order->getError());
        }*/
        $Qrcode = new ExtractQRcode(
            $this->wxapp_id,
            $this->user,
            $order_id,
            OrderTypeEnum::MASTER
        );
        return $this->renderSuccess([
            'qrcode' => $Qrcode->getWxImage(),
        ]);
    }
    /*
     * 获取定位
     */
    public function getLocation(){
        $wxapp_id=input('post.wxapp_id');
        $url=input('post.url');
        if(empty($url)){
            return $this->renderError('url参数不能为空');
        }
        $weixinInfo=Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->find();
        $obj=new jssdkModel($weixinInfo['app_id'],$weixinInfo['app_secret']);
        $info=$obj->getSignPackage($url);
        return $this->renderSuccess($info);
    }

}
